<?php
namespace Admin\Controller;
use Lib\Api\OrganizationSettlementApi;
use Think\Controller;
class OrganizationSettlementController extends Controller {
    private $modelName = 'OrganizationSettlement';

    public function index(){
        $this->display();
    }

    public function tolist() {
        $page_size = $_POST['page_size'];
        $page_num = $_POST['page_num'];
        $organization_id = session('admin_id');

        $field = "a.*, b.name, b.code, b.p_code, b.level, e.name as role_name";

        $where["a.state"] = array("eq", 1);
        $where['_string'] = '(c.belong_organization_id='.$organization_id.' and DATE_FORMAT(c.datetime, "%Y-%m-%d %H:%i:%s") <= DATE_FORMAT("'.date('Y-m-d H:i:s').'", "%Y-%m-%d %H:%i:%s")) or a.organization_id = '.$organization_id;
        $left = 'left join fs_organization b on b.id = a.organization_id';
        $left .= ' left join fs_organization_belong c on c.organization_id = b.id';
        $left .= ' left join fs_organization_role d on d.organization_id = a.organization_id';
        $left .= ' left join fs_role e on e.id = d.role_id';
        $list = OrganizationSettlementApi::getList($where, $left, $page_num, $page_size, $field, 'a.settlement_date desc', 'a.id');

        $model = M();
        $sql = "select count(t.id) total from";
        $sql .= " (select a.id, a.state from fs_organization_settlement a left join fs_organization b on b.id = a.organization_id";
        $sql .= " left join fs_organization_belong c on c.organization_id = b.id left join fs_organization_role d on d.organization_id = a.organization_id left join fs_role e on e.id = d.role_id";
        $sql .= " where (c.belong_organization_id=".$organization_id." and DATE_FORMAT(c.datetime, \"%Y-%m-%d %H:%i:%s\") <= DATE_FORMAT('".date('Y-m-d H:i:s')."', \"%Y-%m-%d %H:%i:%s\")) or a.organization_id = ".$organization_id;
        $sql .= " group by a.id) t where t.state = 1";
        $total = $model->query($sql);
        $user_list = pageHelper($page_size, $page_num, $list, $total[0]["total"]);
        $rel['data'] = $user_list;


        $model = M();
        $sql = "select sum(t.settlement_money) as settlement_money, sum(t.total_fee) as total_fee, sum(t.cash_profit_loss) as cash_profit_loss, sum(t.cash_income) as cash_income from";
        $sql .= " (select a.settlement_money, a.total_fee, a.cash_profit_loss, a.cash_income, a.state from fs_organization_settlement a left join fs_organization b on b.id = a.organization_id";
        $sql .= " left join fs_organization_belong c on c.organization_id = b.id left join fs_organization_role d on d.organization_id = a.organization_id left join fs_role e on e.id = d.role_id";
        $sql .= " where (c.belong_organization_id=".$organization_id." and DATE_FORMAT(c.datetime, \"%Y-%m-%d %H:%i:%s\") <= DATE_FORMAT('".date('Y-m-d H:i:s')."', \"%Y-%m-%d %H:%i:%s\")) or a.organization_id = ".$organization_id;
        $sql .= " group by a.id) t where t.state = 1";
        $sum = $model->query($sql);

        $rel["sum_settlement_money"] = sprintf("%.2f", $sum[0]['settlement_money']);
        $rel["sum_total_fee"] = sprintf("%.2f", $sum[0]['total_fee']);
        $rel["sum_cash_profit_loss"] = sprintf("%.2f", $sum[0]['cash_profit_loss']);
        $rel["sum_cash_income"] = sprintf("%.2f", $sum[0]['cash_income']);

        /*$sum_settlement_money = OrganizationSettlementApi::getSum($where, "a.settlement_money", $left);
        if(empty($sum_settlement_money)) {
            $sum_settlement_money = 0;
        }
        $rel["sum_settlement_money"] = sprintf("%.2f", $sum_settlement_money);

        $sum_total_fee = OrganizationSettlementApi::getSum($where, "a.total_fee", $left);
        if(empty($sum_total_fee)) {
            $sum_total_fee = 0;
        }
        $rel["sum_total_fee"] = sprintf("%.2f", $sum_total_fee);

        $sum_cash_profit_loss = OrganizationSettlementApi::getSum($where, "a.cash_profit_loss", $left);
        if(empty($sum_cash_profit_loss)) {
            $sum_cash_profit_loss = 0;
        }
        $rel["sum_cash_profit_loss"] = sprintf("%.2f", $sum_cash_profit_loss);

        $sum_cash_income = OrganizationSettlementApi::getSum($where, "a.cash_income", $left);
        if(empty($sum_cash_income)) {
            $sum_cash_income = 0;
        }
        $rel["sum_cash_income"] = sprintf("%.2f", $sum_cash_income);*/

        $this->ajaxReturn($rel);
    }
}